﻿using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Text;


public class Version
{

    string _curVersion = "";

    public int maxVersion = 0;

    public int mainVersion = 0;

    public int subVersion = 0;

    public int miniVersion = 0;

    public char[] char_split = new char[]{'.'};

    public Version()
    {
    }

    public Version(string ver)
    {

        curVersion = ver;
    }

    public Version(int ver)
    {
        maxVersion = ver / 1000000;
        mainVersion = ver % 1000000 / 10000;
        subVersion = ver % 1000000 % 10000 / 100;
        miniVersion = ver % 1000000 % 10000 % 100;
    }

    public string curVersion {

        get {

            return new StringBuilder().AppendFormat("{0}.{1}.{2}.{3}", maxVersion, mainVersion, subVersion, miniVersion).ToString();

        }
        set {
            string tmp = value;
            _curVersion = tmp;
            string[] t = tmp.Split(char_split, System.StringSplitOptions.RemoveEmptyEntries);

            if (t.Length > 3) {

                maxVersion = int.Parse(t[0]);
                mainVersion = int.Parse(t[1]);
                subVersion = int.Parse(t[2]);
                miniVersion = int.Parse(t[3]);

            } else if (t.Length > 2) {

                //老版本缺少一个maxVersion
                maxVersion = 0;
                mainVersion = int.Parse(t[0]);
                subVersion = int.Parse(t[1]);
                miniVersion = int.Parse(t[2]);

                _curVersion = new StringBuilder().AppendFormat("{0}.{1}.{2}.{3}", maxVersion, mainVersion, subVersion, miniVersion).ToString();
            }
            
        }
    }

    public string ToString()
    {
         _curVersion = new StringBuilder().AppendFormat("{0}.{1}.{2}.{3}", maxVersion, mainVersion, subVersion, miniVersion).ToString();

         return _curVersion;
    }

    public int ToNumber()
    {
        return maxVersion* 1000000 + mainVersion * 10000 + subVersion * 100 + miniVersion ;
    }

    public int highNum() {

        return maxVersion * 1000000 + mainVersion * 10000;
    }

    public int lowNum() {

        return subVersion * 100 + miniVersion; 
    }

    public bool isLower(Version other)
    {
        //
        return ToNumber() < other.ToNumber();
    }
}

//public enum UpdateVersionState
//{
//    Lastest,
//    DownloadPatch,
//    DownloadInstaller,
//}


public class VersionManager
{

    static VersionManager _instance = new VersionManager();

    public Version version = new Version("0.0.0");

    public static VersionManager Instance
    {

        get
        {
            
#if UNITY_EDITOR


            byte[] content = File.ReadAllBytes(Globals.streamingPath + "/version.txt");

            //// 读取 version.txt(huangyushi,当我们PC上面测试热更新时候。下载下来的文件是放到persistenPath中，所以本地的版本号应该变成下载下来的)
            //byte[] content = File.ReadAllBytes(Globals.persistenPath + "/version.txt");

            string ver = System.Text.Encoding.UTF8.GetString(content);

            _instance.curVersion = ver;

#endif

            return _instance;
        }
    }

    public string curVersion
    {

        get
        {

            return version.curVersion;
        }
        set
        {

            version.curVersion = value;

#if UNITY_EDITOR
            //Debug.Log("<color=red> 保存版本文件  ........当前游戏帧数   = ： </color>" + Time.frameCount);
            saveVersion(Globals.streamingPath + "/version.txt");
#else
            saveVersion(Globals.persistenPath + "/version.txt");
#endif

        }
    }

    public float getVersionNum()
    {
        return version.ToNumber();
    }

    public string getVersionUrl()
    {
        return curVersion.Replace(".", "_");
    }

    public void saveVersion(string path)
    {
        File.WriteAllBytes(path, System.Text.Encoding.UTF8.GetBytes(version.ToString()));
#if Log_Open
        Debug.Log(" Update version info");
#endif
    }

    public void pareVersion() {

        try {

            byte[] ret = File.ReadAllBytes(Application.persistentDataPath + "/version.txt");

            if (ret != null) {
                
                string ver = System.Text.Encoding.UTF8.GetString(ret);

                version.curVersion = ver;
            }
        }
        catch(Exception e){

#if LogError_Open
            Debug.LogError(" pareVersion error " + e);
#endif
        }


    }
}
